<?php
require_once("../../classes/myDatabase.php");

class User extends Database{
     
    public function createUser($fname,$lname,$email,$pass,$gender){
        $this->connectDatabase();
        
        $sQuery = "INSERT INTO account(email,password)
                    VALUES('$email','$pass')";
        mysql_query($sQuery); //EXECUTE SQL
        
        $row = mysql_fetch_array(mysql_query("SELECT account_id FROM account WHERE email = '$email' AND password = '$pass'"));
        $id = $row['account_id'];
        
        $sQuery = mysql_query("UPDATE account SET user_id='$id' WHERE account_id = '$id'");
        
        $sQuery = "INSERT INTO userinfo(fname,lname,gender,account_id)
                    VALUES('$fname','$lname','$gender','$id')";
                    
        mysql_query($sQuery); //EXECUTE SQL
    }
    
    public function loginUser($email,$pass){
        $this->connectDatabase();
        
        $sQuery = "SELECT * FROM account WHERE email='$email' AND password='$pass'";
        $result = mysql_query($sQuery);
        $ctr = 0;
        $row = mysql_fetch_row($result);
        if(!isset($row)){
            $row = null;
        }
        return $row;
    
    }
        
    public function getUser(){
        $this->connectDatabase();
        $sQuery = "SELECT user_id, fname, lname, email, gender FROM registration";
        $result = mysql_query($sQuery);
        $ctr = 0;
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
                $user[$ctr++] = array('user_id'=>$row['user_id'],
                                     'user_lname'=>$row['fname'],
                                     'user_fname'=>$row['lname'],
                                     'user_email'=>$row['email'],
                                     'user_gender'=>$row['gender']);
        }   
        if(!isset($user)){
            $user = null;
        }
        return $user;
    }
}
?>